package com.ahoi.common.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 * </p>
 * Function List:<br>
 * 常用的时间操作
 */
public class DateUtils {

    public static final String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss";
    public static final String FORMAT_HM = "yyyy-MM-dd HH:mm";
    public static final String FORMAT_ = "yyyy-MM-dd";
    public static final String FORMAT_DATE_CN = "yyyy年MM月dd日";
    public static final String FORMAT_DATETIME_CN = "yyyy年MM月dd日 HH时mm分";
    public static final String FORMAT_FULL_CN = "yyyy年MM月dd日 HH时mm分ss秒";
    public static final String FORMAT_YMD = "yyyyMMdd";
    public static final String FORMAT_YMD_HM = "yyyyMMdd HH:mm";
    public static final String FORMAT_ONLY_HM = "HH:mm";
    public static final String FORMAT_ONLY_MM = "MM";
    public static final String FORMAT_ONLY_YYYY = "yyyy";
    public static final String FORMAT_ONLY_MMdd = "MM-dd";
    public static final String FORMAT_ONLY_HH = "HH";

    /**
     * 计算给定日期是星期几
     */
    public static int getWeekOfDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w == 0)
            w = 7;
        return w;
    }

    /**
     * 格式化日期
     */
    public static String formatDate(Date date, String pattern) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    /**
     * 标准化格式化日期时间
     */
    public static String formatDate(Date date) {
        SimpleDateFormat format = new SimpleDateFormat(FORMAT_FULL);
        return format.format(date);
    }

    /**
     * 根据给定日期字符串和日期格式 创建日期
     */
    public static Date createDate(String dateString, String pattern) {
        Date result = null;
        try {
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            result = format.parse(dateString);
        } catch (ParseException e) {
//			e.printStackTrace();
        }
        return result;
    }

    /**
     * 正则匹配时间字符串，转换为对应格式的date对象
     */
    public static Date createDate(String dateString) {
        if (StringUtils.isNotEmpty(dateString)) {
            Date date = matcher(dateString, "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}", FORMAT_FULL);
            if (date == null)
                date = matcher(dateString, "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}", FORMAT_HM);
            if (date == null)
                date = matcher(dateString, "\\d{4}-\\d{2}-\\d{2}", FORMAT_);
            if (date == null)
                date = matcher(dateString, "\\d{4}\\d{2}\\d{2} \\d{2}:\\d{2}", FORMAT_YMD_HM);
            if (date == null)
                date = matcher(dateString, "\\d{4}\\d{2}\\d{2}", FORMAT_YMD);
            if (date == null)
                date = matcher(dateString, "\\d{4}年\\d{2}月\\d{2}日 \\d{2}时\\d{2}分\\d{2}秒", FORMAT_FULL_CN);
            if (date == null)
                date = matcher(dateString, "\\d{4}年\\d{2}月\\d{2}日 \\d{2}时\\d{2}分", FORMAT_DATETIME_CN);
            if (date == null)
                date = matcher(dateString, "\\d{4}年\\d{2}月\\d{2}日", FORMAT_DATE_CN);
            if (date == null)
                date = matcher(dateString, "\\d{4}", FORMAT_ONLY_YYYY);
            return date;
        }
        return null;
    }

    /**
     * 正则匹配时间字符串
     */
    private static Date matcher(String dateString, String regex, String format) {
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(dateString);
        if (m.find()) {
            return createDate(dateString, format);
        }
        return null;
    }

    public static Timestamp getTimeStamp() {
        return new Timestamp(new Date().getTime());
    }

    /**
     * 获取当前日期
     */
    public static Date getDate() {
        return createDate(formatDate(new Date()), DateUtils.FORMAT_);
    }

    /**
     * 获取当前日期
     */
    public static Date getDateTime() {
        return createDate(formatDate(new Date()), DateUtils.FORMAT_FULL);
    }

    public static String getDateTimeString() {
        try {
            return formatDate(getDateTime(), FORMAT_FULL);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 获取当前年yyyy
     */
    public static int getCurrentYearOfDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.YEAR);
    }

    /**
     * 获取本年年份
     */
    public static int getCurrentYear() {
        return getCurrentYearOfDate(DateUtils.getDate());
    }

    // 获取本月月份
    public static int getCurrentMonth() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(DateUtils.getDate());
        return cal.get(Calendar.MONTH);
    }

    // 获取指定年月的第一天 YYYY-MM-DD HH:mm:dd
    public static String getFirstDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(year, month - 1, 1, 0, 0, 0);
        return formatDate(cal.getTime());
    }

    // 获取指定年月的最后一天 YYYY-MM-DD HH:mm:dd
    public static String getLastDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(year, month - 1, 1, 23, 59, 59);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return formatDate(cal.getTime());
    }

    public static String getFirstDayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;

        return DateUtils.getFirstDayOfMonth(year, month);
    }

    public static String getLastDayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;

        return DateUtils.getLastDayOfMonth(year, month);
    }

    // 获取date这一年的第一天 yyyy-MM-dd HH:mm:ss
    public static String getYearFirstDay(String date) {
        return getCurrentYearOfDate(createDate(date)) + "-01-01 00:00:00";
    }

    // 获取date这一年的最后一天 yyyy-MM-dd HH:mm:ss
    public static String getYearLastDay(String date) {
        return getCurrentYearOfDate(createDate(date)) + "-12-31 23:59:59";
    }

    // 获取今日开始时间
    public static Date getTodayStart() {
        return DateUtils.createDate(DateUtils.formatDate(DateUtils.getDate(), DateUtils.FORMAT_) + " 00:00:00");
    }

    // 获取今日结束时间
    public static Date getTodayEnd() {
        return DateUtils.createDate(DateUtils.formatDate(DateUtils.getDate(), DateUtils.FORMAT_) + " 23:59:59");
    }


    public static void main(String[] args) {
        Date date = DateUtils.createDate("2018-11-29");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;

        Date start = DateUtils.createDate(DateUtils.getFirstDayOfMonth(year, month));
        Date end = DateUtils.createDate(DateUtils.getLastDayOfMonth(year, month));
        System.out.println(DateUtils.formatDate(start));
        System.out.println(DateUtils.formatDate(end));
    }
}